package CopyOnWriteArrayList_11;
import java.util.concurrent.CopyOnWriteArrayList;

interface EventListener {
    void onEvent(String event);
}

public class 事件监听器列表_观察者模式实现 {
    private final CopyOnWriteArrayList<EventListener> listeners = new CopyOnWriteArrayList<EventListener>();

    public void addListener(EventListener listener) {
        listeners.add(listener);
    }

    public void removeListener(EventListener listener) {
        listeners.remove(listener);
    }

    public void dispatchEvent(final String event) {
        for (EventListener listener : listeners) {
            listener.onEvent(event);
        }
    }

    public static void main(String[] args) {
        final 事件监听器列表_观察者模式实现 dispatcher = new 事件监听器列表_观察者模式实现();
        final int LISTENER_COUNT = 8;
        final int EVENT_COUNT = 5;

        // 添加监听器
        for (int i = 0; i < LISTENER_COUNT; i++) {
            final int listenerId = i;
            EventListener listener = new EventListener() {
                public void onEvent(String event) {
                    try {
                        System.out.println("监听器" + listenerId + " 处理事件: " + event);
                        Thread.sleep((long)(Math.random() * 30)); // 模拟处理时间
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            dispatcher.addListener(listener);
        }

        // 创建3个线程发送事件
        for (int i = 0; i < 3; i++) {
            final int threadId = i;
            new Thread(new Runnable() {
                public void run() {
                    try {
                        for (int j = 0; j < EVENT_COUNT; j++) {
                            String event = "事件-" + threadId + "-" + j;
                            System.out.println("发送线程" + threadId + " 发送: " + event);
                            dispatcher.dispatchEvent(event);
                            Thread.sleep(100); // 模拟事件间隔
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "事件发送线程-" + i).start();
        }

        // 模拟动态添加/移除监听器
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(300);
                    EventListener tempListener = new EventListener() {
                        public void onEvent(String event) {
                            System.out.println("临时监听器处理: " + event);
                        }
                    };
                    dispatcher.addListener(tempListener);
                    System.out.println("添加了临时监听器");

                    Thread.sleep(400);
                    dispatcher.removeListener(tempListener);
                    System.out.println("移除了临时监听器");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}